/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   RoadNetProjectServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.roadnet    </p>
 * 
 * <p>最后修改 : $: Aug 30, 2017-2:24:27 PM v 1.0.0	 heyong   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.roadnet;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.ReconstructionDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.Reconstruction;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.roadnet.DisasterPreventionService;
import com.pengtu.gsj.service.check.yh.roadnet.RoadNetProjectService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * RoadNetProjectServiceImpl
 * 
 * Aug 30, 2017 2:24:27 PM
 * 
 * @version 1.0.0
 * 
 */
@Repository
@Transactional
public class RoadNetProjectServiceImpl implements RoadNetProjectService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private ReconstructionDao reconstructionDao;
	
	@Autowired
	private DisasterPreventionService disasterPreventionService;
	
	@Override
	public List<Reconstruction> getDisasterPreventionDatas(QueryFilter queryFilter){
		return reconstructionDao.find(queryFilter);
	}
	
	@Override
	public List<Map<String, Object>> yearList(String startTime, String endTime){
		List<Map<String, Object>> yearList = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<Reconstruction> yeaReconstructions = reconstructionDao.getcountReconsGroupByYear(startTime, endTime);
		Double totalSubsidy = 0.0;
		Double totalSelf = 0.0;
		for (Reconstruction reconstruction : yeaReconstructions) {
			map = new HashMap<String, Object>();
			map.put("year", reconstruction.getProjectYear());
			map.put("subsidy", reconstruction.getSubsidyAmount());
			map.put("selfRaised", reconstruction.getSelfRaised());
			map.put("rate",new BigDecimal(Double.parseDouble(reconstruction.getRate())).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			totalSubsidy += reconstruction.getSubsidyAmount();
			totalSelf += reconstruction.getSelfRaised();
			yearList.add(map);
		}
		map = new HashMap<String, Object>();
		map.put("year","小计");
		map.put("subsidy", totalSubsidy);
		map.put("selfRaised", totalSelf);
		map.put("rate",new BigDecimal(totalSelf*100/(totalSelf+totalSubsidy)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		
		yearList.add(map);
		
		return yearList;
	}
	
	
	/**
	 * 路网工程管理的判分和导出
	 */
	@Override
	public Map<String, Object> getDisasterPreventionMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> value = new ArrayList<Object>();
		List<Reconstruction> abList = new ArrayList<Reconstruction>();
		List<Map<String, Object>> yearList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> classifyList = new ArrayList<Map<String,Object>>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//4年配套总比例达到55%得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//4年配套总比例每降低1个百分点扣分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//项目执行情况满分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//每个项目降低1个百分点扣分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		String abProjectId = (String)jsonObject.get("abProjectId");
		String wqProjectId = (String)jsonObject.get("wqProjectId");
		String zhProjectId = (String)jsonObject.get("zhProjectId");
		//获取到国省检时间内各个类型项目的详情
		yearList = this.yearList(startTime, endTime);
		//获取各个类型项目的年度详情
		classifyList = disasterPreventionService.classifyList(startTime, endTime);
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double total = 0; 	//检查得分(默认实地抽查得满分)
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull + poitMark;	//满分
		
		//综合数据得分
		double countMark = 0;
		if (yearList.size() > 0) {
			//获取最后一条小计数据判分
			if (MathsUtils.formatDouble(Double.parseDouble((yearList.get(yearList.size()-1).get("rate").toString())), 0) > 55) {
				total += yearFull;
				countMark += yearFull;
			} else {
				double temp = 55 - MathsUtils.formatDouble(Double.parseDouble((yearList.get(yearList.size()-1).get("rate").toString())), 0);
				total += (yearFull - temp*yearPart) > 0 ? (yearFull - temp*yearPart) : 0 ;
				countMark += (yearFull - temp*yearPart) > 0 ? (yearFull - temp*yearPart) : 0 ;
			}
		}
		
		//数据得分
		boolean flag = false;
		if (StringUtils.isNotEmpty(abProjectId)) {
			value.add(abProjectId);
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", abProjectId, MatchType.EQ);
			abList = this.getDisasterPreventionDatas(queryFilter);
			flag = true;
		} 
		
		if (StringUtils.isNotEmpty(wqProjectId)) {
			value.add(wqProjectId);
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", wqProjectId, MatchType.EQ);
			abList.addAll(this.getDisasterPreventionDatas(queryFilter));
			flag = true;
		} 
		
		if (StringUtils.isNotEmpty(zhProjectId)) {
			value.add(zhProjectId);
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", zhProjectId, MatchType.EQ);
			abList.addAll(this.getDisasterPreventionDatas(queryFilter));
			flag = true;
		} 
		double temp = 0;
		if (flag) {
			for (Reconstruction reconstruction : abList) {
				double rate =  reconstruction.getSelfRaised()*100/reconstruction.getTotalInvestment();
				if (MathsUtils.formatDouble(rate, 0) >= 55  ) {
					total += poitMark/6;
					temp += poitMark/6;
				} else {
					total += (poitMark/6 - (55 - MathsUtils.formatDouble(rate, 0))*reserveMark) > 0 ? (poitMark/6 - (55 - MathsUtils.formatDouble(rate, 0))*reserveMark) : 0 ;
					temp += (poitMark/6 - (55 - MathsUtils.formatDouble(rate, 0))*reserveMark) > 0 ? (poitMark/6 - (55 - MathsUtils.formatDouble(rate, 0))*reserveMark) : 0 ;
				}
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,1));
		//判断完善度
		percentage = MathsUtils.divDouble(MathsUtils.formatDouble(total,2) *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, MathsUtils.formatDouble(total, 1), tableFull, value);
		map.put("yearList", yearList);
		map.put("classifyList", classifyList);
		map.put("percent", percentage);
		map.put("att", list);
		map.put("abList", abList);
		map.put("flag", flag);
		map.put("temp", temp);
		map.put("countMark", countMark);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
